layui.use(['form','laypage','layer'],function(){
	
	var form = layui.form,layer = layui.layer,laypage = layui.laypage;
	
	  form.on('checkbox(selectall-btn)',function(){selectAll();});
	  form.on('checkbox(selectfx-btn)',function(){selectFx();})
	  form.on('checkbox(selectfx-btn)',function(){selectFx();})
	  $('#save-btn').on('click',function(){save();});
	  $("#search-btn").on('click',function(){initPage();});
	  initPage();
	  
	  
	  function getData(pageIndex,pageSize){
	 	  var data = {};
	 	  data["pageIndex"] = pageIndex?pageIndex:1;
	 	  data["pageSize"] = pageSize?pageSize:10;
	 	  var serialNumber = $("#serialNumber").val();
	 	  data["serialNumber"] = serialNumber;
	 	  var brand = $("#brand").val();
	 	  data["brand"] = brand;
	 	  return data;
	   	}
	  
	  
	  function initPage(){
		  var data = getData();
		  ajaxPost("../device/laylist",data,
				  function(data){
			  createData(data.data.list);
			  laypage.render({
				  elem:'page',
				  count:data.data.total,
				  layout: ['count', 'prev', 'page', 'next', 'limit', 'skip']
			    ,jump: function(obj,first){
			    	if(!first){
			    		toPage(obj.curr,obj.limit);
			    	}
			    }
			  });
		  },
				  function(data){
					  layer.alert(data.errorMsg,{
							icon:5,
							title:"错误"
						});
				  });
	  }
	  
	  window.initPage = initPage;
	  function toPage(pageIndex,pageSize){
	 	  
	 	var data = getData(pageIndex,pageSize);
	 	  ajaxPost("../device/laylist",data,
	 			  function(data){createData(data.data.list);},
	 			  function(data){
	 				  layer.alert(data.errorMsg,{
	 						icon:5,
	 						title:"错误"
	 					});
	 			  });
	   }
	  
	  
	  function createData(data){
		  var deviceIds = $("#deviceIds").val();
		  if(isBlank(deviceIds))
			  deviceIds = "";
		  var selIds = deviceIds.split(",");
		  var content = [];
		  for(var i =0 ;i < data.length;i++){
			  var devicedata = data[i];
			  var id = devicedata.id;
			  content.push('<tr>');
			  if(selIds.indexOf(id+"") > -1)
				  content.push('<td><input type="checkbox" name="checkbox"  lay-skin="primary" value="'+devicedata.id+'" checked/></td>');
			  else
				  content.push('<td><input type="checkbox" name="checkbox"  lay-skin="primary" value="'+devicedata.id+'"/></td>');
			  content.push('<td>'+devicedata.serialNumber+'</td>');
			  content.push('<td>'+devicedata.platform+'</td>');
			  content.push('<td>'+devicedata.brand+'</td>');
			  content.push('<td>'+devicedata.model+'</td>');
			  content.push('</tr>');
		  }
		  
		  $("#dg").empty().append(content.join(""));
		  form.render('checkbox');
	  }
	  
	  
	  
	  function save(){
		  
		  var seldata = [];
		  $("input[name='checkbox']:checked").each(function(){
			  var id = $(this).val();
			  var closestTr = $(this).closest("tr");
			  var serialNumber = closestTr.find("td:eq(1)").text();
			  var platform = closestTr.find("td:eq(2)").text();
			  var brand = closestTr.find("td:eq(3)").text();
			  var model = closestTr.find("td:eq(4)").text();
			  seldata.push({"deviceId":id,"serialNumber":serialNumber,"platform":platform,"brand":brand,"model":model});
			  
		  });
		  
		  if(seldata.length == 0){
				  layer.alert("请选择一条设备",{
						icon:5,
						title:"错误"
					});
			  return false;
		  }
		  
		  parent.selDevice(seldata);
		  var idx = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
		  parent.layer.close(idx);
	  }
	  
	 function selectAll(){
		 $("input[name='checkbox']").prop("checked",$("#selectAll").prop("checked"));
		 form.render("checkbox");
	 }
	 
	 function selectFx(){
		 $("input[name='checkbox']").each(function(){
			 if($(this).prop("checked"))
				 $(this).prop("checked",false);
			 else
				 $(this).prop("checked",true);
		 });
		 form.render("checkbox");
	 }
	  
});